<?php include "include/function_library.php"; ?>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>

<?php include "html/header.html"; ?>

</head>
<body class="tcuk home">

	<div id="wp">
	
		<?php  include "html/menu.html"; ?>
		
		<!-- CONTENT STARTS-->
		<div id="ct"> 	
			
			<?php 
			$id = get("id");
			$query = "SELECT total_price FROM tour_cost WHERE tour_id=$id";
			$result = sql_query($query);
			$tuple = mysql_fetch_assoc($result);
				
			$totel_price = $tuple["total_price"];
				
			$hotel_number = sql_number("SELECT hotel_id FROM hotel_of_tour WHERE tour_id=$id");
				
			$transportation_number = sql_number("SELECT transportation_id FROM transportation_of_tour WHERE tour_id=$id");
				
			$query = "SELECT * FROM tour WHERE id=$id";
			$result = sql_query($query);
			$tuple = mysql_fetch_assoc($result);
			
			if(post("tp_submit")) {
				$tp_payment_type = post("tp_payment_type");
				$tp_user_id		 = session("id");
				$tp_date	 	 = date("Y-m-d");
				$tp_id		 	 = post("tp_id");
				
				$link = connect();
				$add_purchase_query = "INSERT INTO purchase (user_id, date, payment_type) VALUES ('$tp_user_id', '$tp_date', '$tp_payment_type')";
				$result_tour_purchase = mysql_query($add_purchase_query, $link) or die("Sql Error! " . mysql_error());
				$last_added_purchase_id = mysql_insert_id();
				
				for ($i=1; $i<$hotel_number; $i++) {
				  	$hp_room_number  = post("hp_room_number".$i);
					$hp_is_honeymoon = post("hp_is_honeymoon".$i);
					$hp_start_date   = post("hp_start_date".$i);
					$hp_end_date  	 = post("hp_end_date".$i);
					$hp_hotel_id	 = post("hp_hotel_id".$i);
					
					$query_hotel_purchase = "INSERT INTO hotel_purchase (purchase_id, hotel_id, room_number, is_honeymoon, start_date, end_date)
							  				 VALUES ('$last_added_purchase_id', '$hp_hotel_id', '$hp_room_number', '$hp_is_honeymoon', '$hp_start_date', '$hp_end_date')";
					$result_hotel_purchase = $result_tour_purchase && (mysql_query($query_hotel_purchase, $link) or die("Sql Error! " . mysql_error()));
				}
				
				for ($i=1; $i<$transportation_number; $i++) {
					$tp_seat_number   	  = post("tp_seat_number".$i);
					$tp_transportation_id = post("tp_transportation_id".$i);
						
					$query_transportation_purchase = "INSERT INTO transpor_purchase (purchase_id, transportation_id, seat_number)
													  VALUES ('$last_added_purchase_id', '$tp_transportation_id', '$tp_seat_number')";
					$result_transportation_purchase = $result_tour_purchase && (mysql_query($query_transportation_purchase, $link) or die("Sql Error! " . mysql_error()));
				}
				
				
				$query_tour_purchase = "INSERT INTO tour_purchase (purchase_id, tour_id) VALUES ('$last_added_purchase_id', '$tp_id')";
				$result_tour_purchase2 = $result_tour_purchase && (mysql_query($query_tour_purchase, $link) or die("Sql Error! " . mysql_error()));
				mysql_close($link);
				
				if($result_tour_purchase2) {
					redirect("tour_detail.php?purchased=1&id=$tp_id");
				} else {
					echo "<div class='alert error'>Error! Could Not Purchase! There is(are) error(s) in the form!</div>";
				}

			} 
			?>
			<div style="width: 70%; text-align: center; margin-top:30px; margin-left:auto; margin-right:auto;">
				<h3>
					Your are purchasing tour ticket 
					for <?php echo $tuple["name"]; ?>. 
					This tour includes <?php echo $hotel_number; ?> hotel ticket(s) 
					and <?php echo $transportation_number; ?> transportation ticket(s)
					with a total price of <?php echo $totel_price; ?>.
				</h3>
			</div>
			<form action="<?php echo curPageURL(); ?>?id=<?php echo $id ?>" method="post">
			<ul id="signup_form" style="width: 50%; margin-left: auto; margin-right: auto; margin-top: 30px; list-style: none;">
				<?php 
					$hotel_index = 1;
					$result_hotel = sql_query("SELECT hotel_id FROM hotel_of_tour WHERE tour_id=$id");
					while($tuple_hotel = mysql_fetch_assoc($result_hotel)) {
						?>
						<li>
							<label>Room Number for <?php echo $hotel_index; ?>. Hotel </label>
							<input type="number" id="hp_room_number<?php echo $hotel_index; ?>" name="hp_room_number<?php echo $hotel_index; ?>" placeholder="Room number.." style="width: 250px;"/>
						</li>
						<li>
							<label>Is Honeymoon for <?php echo $hotel_index; ?>. Hotel </label>
							<input type="radio" id="hp_is_honeymoon<?php echo $hotel_index; ?>" name="hp_is_honeymoon<?php echo $hotel_index; ?>" value="1"/>Yes, it is.  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
							<input type="radio" id="hp_is_honeymoon<?php echo $hotel_index; ?>" name="hp_is_honeymoon<?php echo $hotel_index; ?>" value="0" checked/>No, it is not.
						</li>
						<li>
							<br /><label>Start Date for <?php echo $hotel_index; ?>. Hotel </label>
							<input type="date" id="hp_start_date<?php echo $hotel_index; ?>" name="hp_start_date<?php echo $hotel_index; ?>" style="width: 250px;"/>
						</li>
						<li>
							<label>End Date for <?php echo $hotel_index; ?>. Hotel </label>
							<input type="date" id="hp_end_date<?php echo $hotel_index; ?>" name="hp_end_date<?php echo $hotel_index; ?>" style="width: 250px;"/>
							
							<input type="hidden" name="hp_hotel_id<?php echo $hotel_index; ?>" value="<?php echo $tuple_hotel["hotel_id"] ?>"/>
						</li>
						<?php
						$hotel_index++;
					}
				?>
				<?php 
					$transportation_index = 1;
					$result_transportation = sql_query("SELECT transportation_id FROM transportation_of_tour WHERE tour_id=$id");
					while($tuple_transportation = mysql_fetch_assoc($result_transportation)) {
						?>
						<li>
							<label>Seat Number for <?php echo $transportation_index; ?>. Transportation </label>
							<input type="number" id="tp_seat_number<?php echo $transportation_index; ?>" name="tp_seat_number<?php echo $transportation_index; ?>" placeholder="Seat number.." style="width: 250px;"/>
						
							<input type="hidden" name="tp_transportation_id<?php echo $transportation_index; ?>" value="<?php echo $tuple_transportation["transportation_id"]; ?>"/>
						</li>
						<?php
						$transportation_index++;
					}
				?>
				<li>
					<label>Payment Type </label>
					<select id="tp_payment_type" name="tp_payment_type" style="width: 265px;">
						<option value="Credit Card">Credit Card</option>
						<option value="Cash">Cash</option>
						<option value="Paypall">Paypall</option>
						<option value="Bitcoin">Bitcoin</option>
						<option value="EFT">EFT</option>
					</select>
				</li>
				<li>
					<input type="hidden" name="tp_id" value="<?php echo $id ?>"/>
					<input type="submit" id="tp_submit" name="tp_submit" value="Purchase" style="width: 90px; height:30px; margin-left: 80px;"/>
				</li>
			</ul>
			</form>
		</div> 	
		<!-- CONTENT ENDS-->		
		
		<?php include "html/footer.html"; ?>
		
	</div>
	
</body>
</html>